﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;
using RBS.Common;
using RBS.Web.API.Models;
using JsonData = RBS.Common.JsonEntity.JsonData;

namespace RBS.Web.API.Areas.Deed.Controllers
{
    /// <summary>
    /// 结算
    /// </summary>
    public class SettlementController : ApiController
    {
        private ApiContext db = new ApiContext();
        // GET api/settlement 
        public JsonData Get(int uid, int hid,int oid=0)
        {
            var jsonData = new JsonData();
            try
            {
                var bll = new RBS.Core.BLL.P_Advertis_Payment();
                string strWhere = " and U_Id=" + uid + " and HotelId=" + hid; 
                const string orderby = " id  desc";
                var startIndex = 1;
                int totalItems = 0; //要分页的总记录数
                DataTable dt = bll.GetListByUser(10, startIndex, strWhere, orderby, out totalItems).Tables[0];
                IList<object> coupons = new List<object>();
                foreach (DataRow row in dt.Rows)
                {
                    coupons.Add(new
                    {
                        Id = row["id"],
                        batchId = row["batchId"],
                        DeedType = row["DeedType"],
                        Denomination = row["Denomination"],
                        HotelName = new RBS.Core.BLL.Hotel.H_Hotels().GetModelByCache(Convert.ToInt32(row["HotelId"])).H_Name,
                        DeedStatus = GetDeedStatus(Convert.ToInt32(row["DeedStatus"])),
                        UseRules = row["UseRules"],
                        Crowd = row["Crowd"]
                    });
                }  
                //积分 
                IList<object> integration = new List<object>();
                var item = db.U_User_Integration.Where(c => c.UserId == uid).Sum(c => c.Point);
                integration.Add(new
                {
                    Id = -1,
                    batchId =0,
                    DeedType =0,
                    Denomination = item,
                    HotelName = "",
                    DeedStatus ="正常",
                    UseRules = 0,
                    Crowd = "当前可用积分为" + item+"分，可抵用"+ item/100.00 +"元"
                });
                 
                var list = new Settlement
                    {
                        Coupons = coupons,
                        Integration = integration[0]
                    }; 

                jsonData.Success = true;
                jsonData.Data = list; 
            }
            catch (Exception ex)
            {

                jsonData.Success = false;
                jsonData.Data = null; 
                jsonData.Page = 0;
                jsonData.TotalPage = 0;
                jsonData.Error = ex.Message;
            }
            return jsonData;
        }



        //优惠券状态
        private string GetDeedStatus(int status)
        {
            var str = "";
            Type t = typeof(RBS.Common.RBSEnums.DeedStatus);
            var arrays = Enum.GetValues(t);
            for (int i = 0; i < arrays.LongLength; i++)
            {
                var test = (RBS.Common.RBSEnums.DeedStatus)arrays.GetValue(i);

                if ((int)test != status) continue;

                FieldInfo fieldInfo = test.GetType().GetField(test.ToString());
                object[] attribArray = fieldInfo.GetCustomAttributes(false);
                var attrib = (EnumDescriptionAttribute)attribArray[0];
                str = attrib.Description;
            }
            return str;
        }
    }
}
